package com.formiaomiao.demain.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.util.ToastUtils;

public class PublishWindow implements View.OnClickListener {

    Context context;
    PopupWindow pop;

    EditText et_publish;
    Button btn_publish;

    public PublishClickListener publishClickListener;

    public interface PublishClickListener {
        void onPublishClickListener(View v, String data);
    }

    public void setOnPublishClickListener(PublishClickListener publishClickListener) {
        this.publishClickListener = publishClickListener;
    }

    public PublishWindow(Context context, PopupWindow pop) {
        this.context = context;
        this.pop = pop;
    }

    /**
     * 显示选择pop
     */
    public void showPicPopWindow(View parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.pop_publish_layout, null);
        initView(view);
        // 设置SView
        pop.setContentView(view);
        // 设置弹出窗体的宽
        pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体的高
        pop.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        pop.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置弹出窗体的背景
        pop.setBackgroundDrawable(dw);
        // 获取当前打开的界面的 父View
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        final PopupWindow finalPop = pop;
        view.setOnTouchListener(new View.OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            public boolean onTouch(View v, MotionEvent event) {
                finalPop.dismiss();
                return true;
            }
        });
        pop.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    private void initView(View view) {
        et_publish = view.findViewById(R.id.et_publish);
        btn_publish = view.findViewById(R.id.btn_publish);
        setEditTextHint("");
        btn_publish.setOnClickListener(this);
    }

    public void setEditTextHint(String hint) {
        et_publish.setHint("回复" + hint);
    }

    @Override
    public void onClick(View view) {
        if (et_publish.getText().toString().isEmpty()) {
            ToastUtils.show(context, "请输入评论内容");
        } else {
            publishClickListener.onPublishClickListener(view, et_publish.getText().toString().trim());
        }
    }

}
